//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SVD.h"
using namespace Cephei::QL::Math::Matrixutilities;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (Cephei::QL::Math::IMatrix^ M) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _CM;
    try
    {
#ifdef HANDLE
        _phSVD = NULL;
#endif
        _CM = safe_cast<CMatrix^> (M);
        _CM->Lock();
        QuantLib::Matrix& _M = static_cast<QuantLib::Matrix&> (_CM->GetReference ()); 
        _ppSVD = new boost::shared_ptr<QuantLib::SVD> (new QuantLib::SVD ( _M ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CM != nullptr) _CM->Unlock();
    }
}
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (boost::shared_ptr<QuantLib::SVD>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSVD = NULL;
#endif
	_ppSVD = &childNative;
    
}
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (QuantLib::SVD& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSVD = NULL;
#endif
	_ppSVD = new boost::shared_ptr<QuantLib::SVD> (&childNative);
    
    _SVDOwner = owner;
    
}

Cephei::QL::Math::Matrixutilities::CSVD::CSVD (CSVD^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSVD = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSVD = new boost::shared_ptr<QuantLib::SVD> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSVD = NULL;
#endif
	if (!t->IsSubclassOf(CSVD::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (QuantLib::Handle<QuantLib::SVD>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSVD = &childNative;
	_ppSVD = &static_cast<boost::shared_ptr<QuantLib::SVD>>(childNative.currentLink());
    
    _SVDOwner = owner;
}
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (QuantLib::Handle<QuantLib::SVD> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSVD = &childNative;
	_ppSVD = &static_cast<boost::shared_ptr<QuantLib::SVD>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Matrixutilities::CSVD::CSVD (QuantLib::SVD childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSVD = NULL;
#endif
	_ppSVD = new boost::shared_ptr<QuantLib::SVD> (new QuantLib::SVD (childNative));
    
}
#endif

Cephei::QL::Math::Matrixutilities::CSVD::~CSVD ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSVD != NULL)
    {
	    delete _ppSVD;
        _ppSVD = NULL;
    }
}
Cephei::QL::Math::Matrixutilities::CSVD::!CSVD ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSVD != NULL)
    {
	    delete _ppSVD;
    }
}
QuantLib::SVD& Cephei::QL::Math::Matrixutilities::CSVD::GetReference ()
{
    if (_ppSVD == NULL) throw gcnew NativeNullException ();
	return **_ppSVD;
}
boost::shared_ptr<QuantLib::SVD>& Cephei::QL::Math::Matrixutilities::CSVD::GetShared ()
{
    if (_ppSVD == NULL) throw gcnew NativeNullException ();
	return *_ppSVD;
}
QuantLib::SVD* Cephei::QL::Math::Matrixutilities::CSVD::GetPointer ()
{
    if (_ppSVD == NULL) throw gcnew NativeNullException ();
	return &**_ppSVD;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SVD>& Cephei::QL::Math::Matrixutilities::CSVD::GetHandle ()
{
	if (_phSVD == NULL)
	{
		_phSVD = new Handle<QuantLib::SVD> (*_ppSVD);
	}
	return *_phSVD;
}
#endif
bool Cephei::QL::Math::Matrixutilities::CSVD::HasNative () 
{
	return (_ppSVD != NULL);
}

Double Cephei::QL::Math::Matrixutilities::CSVD::Cond::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSVD)->cond ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Matrixutilities::CSVD::Norm2::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSVD)->norm2 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Math::Matrixutilities::CSVD::Rank::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppSVD)->rank ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Matrixutilities::CSVD::SingularValues::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppSVD)->singularValues ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Matrixutilities::CSVD::U::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppSVD)->U ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Matrixutilities::CSVD::V::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppSVD)->V ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Matrixutilities::ISVD^ Cephei::QL::Math::Matrixutilities::CSVD_Factory::Create (Cephei::QL::Math::IMatrix^ M)
{
    return gcnew CSVD ( M);
}
